/*
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *      
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *      
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */

#ifndef _MDIWINDOW_H
#define _MDIWINDOW_H

#include <fstream>
#include <sstream>

#include <QApplication>
#include <QFrame>
#include <QMainWindow>
#include <QMdiSubWindow>
#include <QMdiArea>
#include <QFileInfo>
#include <QMessageBox>
#include <QBoxLayout>
#include <QResizeEvent>
#include <QCloseEvent>
#include <QSize>
#include <QCloseEvent>
#include <QFileSystemWatcher>
#include <QMdiSubWindow>
#include <QtGlobal>

#include "../reader.h"
#include "../lexical.h"
#include "../syntax.h"
#include "../gType.h"
#include "../serialize.h"
#include "../transformation.h"
#include "drawArea.h"
#include "bakalarka.h"
#include "gObjects.h"
#include "controlPoints.h"
#include "dialogs.h"

class bakalarka;
class containerModel;

class mdiWindow: public QFrame{

	Q_OBJECT

	private:
		static const int COPY_BUFFER_LEN = 1024;
	
		struct backupItem{
			unsigned long startOffset;
			unsigned long length;
		};

		QString curFile;
		gObjectContainer container;
		drawArea *area;
		bakalarka *mainWin;
		containerModel *model;
		QFileSystemWatcher fileWatcher;
		bool force;
		
		QString strippedName(const QString &fullFileName);
		QString userFriendlyCurrentFile();
		bool copyFiles(std::stringstream &, QString &) const;
		bool readFile(const QString &);
		void offsetBackup(QList<backupItem> &) const;
		void flushBackup(QList<backupItem> &) const;

	protected:
		void resizeEvent(QResizeEvent *);
		
	signals:
		void removeSubWindow(QMdiSubWindow *);
		void reloadSubWindow(QMdiSubWindow *);
		void saveWindowHistory(mdiWindow &);
        void closeSignal(containerModel *model);
	
	public slots:
		void modified(drawArea *);
		void fileModified(const QString &);
		void turnOffWatcher();
		
	protected:
		void closeEvent(QCloseEvent *);

	public:
		mdiWindow(bakalarka *);
		
		bool loadSourceFile(const QString &);
		void setCurrentFile(const QString &);
		QString &currentFile();
		containerModel *getModel();
		gObject *getActiveObject();
		drawArea *getDrawArea();
		bool save(QString);
		bool saveAs();
		gObjectContainer *getContainer();

};

#endif
